package com.boang.controller.parking;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boang.entity.PO.Parkinglot;
import com.boang.resp.Result;
import com.boang.service.ParkinglotService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*
@author:22567
@date:2025/3/10 19:17
*/
@Tag(name = "停车场管理", description = "停车场相关接口")
@RestController
@RequestMapping("/parking")
public class ParkingLotController {
    @Autowired
    private ParkinglotService parkinglotService;

    //-------------------添加--------------------
    @Operation(summary = "添加停车场")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Parkinglot parkinglot) {
        return Result.ok(parkinglotService.save(parkinglot));
    }

    //-------------------修改--------------------
    @Operation(summary = "修改场地")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody Parkinglot parkinglot) {
        return Result.ok(parkinglotService.updateById(parkinglot));
    }

    //-------------------删除--------------------
    @Operation(summary = "删除场地")
    @DeleteMapping("/remove")
    public Result<Boolean> remove(int id) {
        return Result.ok(parkinglotService.removeById(id));
    }

    //-------------------查询--------------------

    @Operation(summary = "条件查询停车场")
    @PostMapping("/page")
    public IPage<Parkinglot> page(@RequestBody Parkinglot parkinglot) {
        QueryWrapper<Parkinglot> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.lambda()
                .eq(parkinglot.getId() != 0, Parkinglot::getId, parkinglot.getId())
                .like(parkinglot.getNumber() != null, Parkinglot::getNumber, parkinglot.getNumber());

        if (parkinglot.getPageNum() < 1) parkinglot.setPageNum(1);
        if (parkinglot.getPageSize() < 1 || parkinglot.getPageSize() > 100) parkinglot.setPageSize(10);
        Page<Parkinglot> page = new Page<>(parkinglot.getPageNum(), parkinglot.getPageSize());
        return parkinglotService.page(page, QueryWrapper);

    }
}
